package net.orange.console.genius;

import net.orange.architecture.process.ProcessFactory;
import net.orange.framework.logic.IUserProcess;
import net.orange.framework.model.User;

/**
 * User genius is the delegate of IDepartmentProcess object in system console.
 * it can call the IUserProcess object directly in scripting language directly,
 * but can through the user genius using the "$user" in program.
 */
public class UserGenius {

	/**
	 * The unique instance of user genius in system.
	 */
	private static UserGenius instance = null;

	/**
	 * The internal IUserProcess object
	 */
	private IUserProcess process;

	/**
	 * Disabled the default constructor, it need to get object instance through
	 * the getInstance method.
	 */
	private UserGenius() {
		super();
	}

	/**
	 * The private constructor with IUserProcess object.
	 * 
	 * @param process
	 *            the user process.
	 */
	private UserGenius(IUserProcess process) {
		this.process = process;
	}

	/**
	 * Get the unique user genius instance.
	 * 
	 * @return The the department genius instance.
	 * @throws Exception
	 */
	public static UserGenius getInstance() throws Exception {
		if (instance == null) {
			IUserProcess _process = (IUserProcess) ProcessFactory
					.load(User.class);
			instance = new UserGenius(_process);
		}
		return instance;
	}

	/**
	 * Retrieve the user according the user code.
	 * 
	 * @param code
	 *            The target user code.
	 * @return The user.
	 * @throws Exception
	 */
	public User find(String code) throws Exception {
		return (User) process.find("user_code", code);
	}
}
